<?php
/**
 * Created by PhpStorm.
 * User: eddy
 * Date: 2017/3/11
 * Time: 23:04
 */

namespace iBrand\Shop\Backend\Http\Controllers\Promotion;

use iBrand\Store\Backend\Service\GoodsService;
use Illuminate\Http\Request;
use iBrand\Store\Backend\Repositories\GoodsRepository;
use iBrand\Backend\Http\Controllers\Controller;
use iBrand\Store\Backend\Facades\ExcelExportsService;
use iBrand\Shop\Backend\Repository\StoreRepository;
use Response;
use Encore\Admin\Facades\Admin as LaravelAdmin;
use Encore\Admin\Layout\Content;

class PublicController extends Controller
{
    protected $goodsRepository;
    protected $goodsService;
    protected $storeRepository;
    protected $o2oGoodsService;

    public function __construct(GoodsRepository $goodsRepository,
                                GoodsService $goodsService,
                                StoreRepository $storeRepository,
                                \iBrand\Shop\Server\Service\GoodsService $o2oGoodsService)
    {
        $this->goodsRepository = $goodsRepository;
        $this->goodsService = $goodsService;
        $this->storeRepository = $storeRepository;
        $this->o2oGoodsService = $o2oGoodsService;
    }

    public function getSpu(Request $request)
    {
        $url = 'admin.shop.promotion.getSpuData';
        $action = $request->input('action');

        return view('shop-backend::promotion.public.modal.getSpu', compact('goods', 'url', 'action'));
    }

    public function getShop(Request $request)
    {
        $url = 'admin.shop.promotion.getShopData';
        $action = $request->input('action');

        return view('shop-backend::promotion.public.modal.getShop', compact('goods', 'url', 'action'));
    }

    public function getSpuData(Request $request)
    {
        $ids = [];
        if ($request->input('ids')) {
            $ids = explode(',', $request->input('ids'));
        }

        $action = $request->input('action');

        $goods_ids = [];
        if ($action == 'view' OR $action == 'view_exclude') {
            $goods_ids = array_merge($goods_ids, $ids);
        }

        $shop_id = session('shop_id');
        $keyWord = request('value');
        $o2oGoods = $this->o2oGoodsService->getShopGoodsByCredentials($shop_id, $keyWord, $ids);
        $goods['data'] = $o2oGoods;
        $goods['ids'] = $ids;

        return $this->ajaxJson(true, $goods);
    }

    public function getShopData(Request $request)
    {
        $ids = explode(',', $request->input('ids'));
        $action = $request->input('action');

        $where = [];
        if (!empty(request('value')) AND request('field') == 'name') {
            $where[request('field')] = ['like', '%' . request('value') . '%'];
        }

        $shops_ids = [];
        if (request('field') == 'category' && !empty(request('value'))) {
            $shops_ids = $this->goodsService->categoryGetGoodsIds(request('value'));
        }

        if ($action == 'view' || $action == 'view_exclude') {
            $shops_ids = array_merge($shops_ids, $ids);
        }

        $shops = $this->storeRepository->getStorePaginated($where, 15, $shops_ids)->toArray();
        if (count($shops) == 0) {
            $shops['data'] = [];
        }

        $shops['ids'] = $ids;

        return $this->ajaxJson(true, $shops);
    }
}